Page({
  data: {
    cartItems: [],
    cardgood:{},
    totalPrice: 0,
    pendingOrder:[],
    isAllSelected: false
  },

  onLoad: function () {
    //this.data.cartItems=wx.getStorageSync('cartItems') || [];
    this.setData({
      cartItems: wx.getStorageSync('cartItems') || []
    });
    console.log(this.data.cartItems,'购物车SP')
    this.calculateTotalPrice();
  },
  onShow(){
    this.setData({
      cartItems: wx.getStorageSync('cartItems') || []
    });
    this.calculateTotalPrice();
    console.log(this.data.cartItems,'购物车SP1')
    
  },

  deleteItem: function (e) {
    const id = e.currentTarget.dataset.id; // 获取商品ID
    let cartItems = this.data.cartItems.filter(item => item.goodsId !== id); // 过滤掉要删除的商品
    this.setData({
      cartItems: cartItems
    });
    this.calculateTotalPrice(); // 重新计算总价
    wx.setStorageSync('cartItems', cartItems); // 更新本地存储
  },

  selectItem: function (e) {
    const id = e.currentTarget.dataset.goodsId;
    console.log(e);
    const cartItem = this.data.cartItems[id];
    // console.log(cartItem);
    cartItem.checked = !cartItem.checked;
    this.setData({
      cartItems: this.data.cartItems
    });
    this.calculateTotalPrice();
    this.checkAllSelected();
  },

  selectAll: function (e) {
    const checked = e.detail.value.length > 0;
    this.setData({
      isAllSelected: checked,
      cartItems: this.data.cartItems.map(item => {
        item.checked = checked;
        return item;
      })
    });
    this.calculateTotalPrice();
  },

  changeQuantity: function(e) {
    const id = e.currentTarget.dataset.id; // 获取商品ID
    const type = e.currentTarget.dataset.type; // 获取操作类型（'plus' 或 'minus'）
    let cartItems = this.data.cartItems.map(item => {
      if (item.goodsId === id) {
        // 根据操作类型增加或减少数量
        if (type === 'plus') {
          return { ...item, quantity: item.quantity + 1 };
        } else if (type === 'minus') {
          // 确保数量不会小于1
          return item.quantity > 1 ? { ...item, quantity: item.quantity - 1 } : item;
        }
      }
      return item;
    });

    // 更新页面数据
    this.setData({
      cartItems: cartItems
    });
    // 立即调用 calculateTotalPrice 方法来重新计算总价
    this.calculateTotalPrice();
    wx.setStorageSync('cartItems', cartItems);
  },
  calculateTotalPrice: function () {
    let cartItems = wx.getStorageSync('cartItems') || [];
    let totalPrice = 0;
    this.data.cartItems.forEach(item => {
      if (item.checked) {
        totalPrice += item.price * item.quantity;
      }
    });
    this.setData({
      totalPrice: totalPrice.toFixed(2)
    });
  },

  checkAllSelected: function () {
    const allSelected = this.data.cartItems.every(item => item.checked);
    this.setData({
      isAllSelected: allSelected
    });
  },

  onClickButton: function () {
    // 这里可以处理结算逻辑，例如跳转到订单确认页面等
    console.log('结算');
    // 获取所有选中的商品
    let selectedItems = this.data.cartItems.filter(item => item.checked);
    
    for (let i = 0; i < selectedItems.length; i++) {
      selectedItems[i].status = '待付款';
    }
    // 将选中的商品信息存储到本地存储中
    wx.setStorageSync('pendingOrder', selectedItems);
    console.log(selectedItems)
    
    // 跳转到待付款订单页面
    wx.navigateTo({
      url: '/packageC/pages/order/order'
    });
  }
});
